<?php

class model_product extends database_mysql {

    public function mdp_get_dataselectbox($seokey) {
        $tb_cat = prefix . 'categories';
        $tb_cat_lang = prefix . 'categories_lang';
        $strSql = "SELECT
                        cat.ID,
                        cal.CatName
                        FROM
                        $tb_cat_lang AS cal ,
                        $tb_cat AS cat
                        WHERE
                        cat.PID = 2 AND
                        cat.Define = 'sub' AND
                        cat.Com = (( 
                                    SELECT
                                            cat1.Com
                                    FROM
                                            $tb_cat AS cat1 ,
                                            $tb_cat_lang AS cal1
                                    WHERE
                                    cal1.SeoKey = '" . $seokey . "' AND
                                    cat1.Define = 'com' AND
                                    cat1.ID = cal1.CatID
                            )) AND
                        cal.LangID = " . LANG . " AND
                        cal.CatID = cat.ID";
        $row = parent::mysql_fetch_all_rows($strSql);
        parent::mysql_close_connection();
        return $row;
    }

    public function mdp_list_location() {
        $strSql = "SELECT 
                            *
                    FROM
                        " . prefix . "location loc
                    WHERE
                        loc.Status = 'true'
                    ORDER BY  loc.Order ";

        $row = parent::mysql_fetch_all_rows($strSql);
        parent::mysql_close_connection();
        return $row;
    }

    public function mdp_product_list($cid = null, $page = 1, $limit = 50) {
        $str = null;
        if (!empty($limit)) {
            $page = ($page == 0 || empty($page) ? 1 : $page);
            $start = ($page - 1) * $limit;
            $strlimit = "LIMIT $start, $limit";
        } else {
            $strlimit = '';
        }

        if ($cid) {
            $cid = parent::mysql_check_string($cid);
            $str = " " . prefix . "product.CID = '" . $cid . "' AND ";
        }
        $strSql = "
                SELECT *
                FROM
                " . prefix . "product INNER JOIN " . prefix . "product_lang 
                ON " . prefix . "product.ID = " . prefix . "product_lang.ProductID
                WHERE
                " . $str . "
                " . prefix . "product_lang.LangID = '" . LANG . "'
                ORDER BY " . prefix . "product.ID DESC " . $strlimit . "
        ";
        var_dump($strSql);
        $row = parent::mysql_fetch_all_rows($strSql);
        parent::mysql_close_connection();
        return $row;
    }

    public function mdp_product_list_search($key = null, $cid = 0, $page = 1, $limit = 50) {
        $str = null;
        if (!empty($limit)) {
            $page = ($page == 0 || empty($page) ? 1 : $page);
            $start = ($page - 1) * $limit;
            $strlimit = "LIMIT $start,$limit ";
        } else {
            $strlimit = '';
        }
        if ($key) {
            $key = parent::mysql_check_string($key);

            $str .= " " . prefix . "product_lang.Name LIKE '%" . $key . "%' AND ";
        }
        if ($cid > 0) {
            $str .= " " . prefix . "product.CID = '" . $cid . "'  AND ";
        }
        $strSql = "
                SELECT *
                FROM
                " . prefix . "product INNER JOIN " . prefix . "product_lang 
                ON " . prefix . "product.ID = " . prefix . "product_lang.ProductID
                WHERE
                " . $str . "
                " . prefix . "product_lang.LangID = '" . LANG . "'
                ORDER BY " . prefix . "product.ID DESC " . $strlimit . " ";
        $row = parent::mysql_fetch_all_rows($strSql);
        parent::mysql_close_connection();
        return $row;
    }

    public function mdp_product_total_list($cid = null) {
        $str = null;
        if ($cid) {
            $cid = parent::mysql_check_string($cid);
            $str = " " . prefix . "product.CID = '" . $cid . "'  AND ";
        }
        $row = parent::mysql_fetch_all_rows("
                SELECT
                count(ID) as tsd
                FROM
                " . prefix . "product INNER JOIN " . prefix . "product_lang 
                ON " . prefix . "product.ID = " . prefix . "product_lang.ProductID
                WHERE
                " . $str . "
                " . prefix . "product_lang.LangID = '" . LANG . "'
                ORDER BY " . prefix . "product.ID DESC
        ");
        $tsd = isset($row[0]['tsd']) ? $row[0]['tsd'] : 0;

        parent::mysql_close_connection();

        return $tsd;
    }

    public function mdp_product_total_list_search($key = null, $cid = 0) {
        $str = null;
        if ($key) {

            $key = parent::mysql_check_string($key);
            $key == 'all' ? $key = '' : $key = $key;
            $str .= " " . prefix . "product_lang.Name LIKE '%" . $key . "%' AND ";
        }
        if ($cid > 0) {
            $str .= " " . prefix . "product.CID = '" . $cid . "'  AND ";
        }
        $row = parent::mysql_fetch_all_rows("
                SELECT
                count(ID) as tsd
                FROM
                " . prefix . "product INNER JOIN " . prefix . "product_lang 
                ON " . prefix . "product.ID = " . prefix . "product_lang.ProductID
                WHERE
                " . $str . "
                " . prefix . "product_lang.LangID = '" . LANG . "'
                ORDER BY " . prefix . "product.ID DESC
        ");
        $tsd = isset($row[0]['tsd']) ? $row[0]['tsd'] : 0;

        parent::mysql_close_connection();

        return $tsd;
    }

    public function mdp_product_get_cid($seokey = null) {
        $str = null;
        $row = parent::mysql_fetch_one_rows("
                SELECT
                *
                FROM
                " . prefix . "product INNER JOIN " . prefix . "product_lang 
                ON " . prefix . "product.ID = " . prefix . "product_lang.ProductID
                WHERE
                " . prefix . "product_lang.SeoKey = '" . $seokey . "'
                " . prefix . "product_lang.LangID = '" . LANG . "'
        ");

        parent::mysql_close_connection();

        return $row;
    }

    public function mdc_get_cat_cid($seokey) {
        $row = null;
        if (!empty($seokey)) {
            $seokey = parent::mysql_check_string($seokey);
            $row = parent::mysql_fetch_one_rows("SELECT  * FROM " . prefix . "categories_lang WHERE SeoKey = '" . trim($seokey) . "' AND LangID = '" . LANG . "'");

            parent::mysql_close_connection();
        }
        return $row;
    }

    public function mdp_get_art($artid) {
        $artid = parent::mysql_check_string($artid);

        $row = parent::mysql_fetch_one_rows("
            SELECT * FROM
                " . prefix . "product
            WHERE
                " . prefix . "product.ID = '" . trim($artid) . "'
        ");

        parent::mysql_close_connection();

        return $row;
    }

    public function mdp_get_prot_lang($artid, $lang_id) {
        $artid = parent::mysql_check_string($artid);

        $row = parent::mysql_fetch_one_rows("
            SELECT * FROM
                " . prefix . "product_lang
            WHERE
                " . prefix . "product_lang.ProductID = '" . trim($artid) . "' AND
                " . prefix . "product_lang.LangID = '" . $lang_id . "'
        ");

        parent::mysql_close_connection();

        return $row;
    }

    public function mdp_get_seo_key($seokey, $lang_id, $artid = null) {
        $str = null;
        $artid = parent::mysql_check_string($artid);
        $seokey = parent::mysql_check_string($seokey);
        if (!empty($artid)) {
            $str = prefix . "product_lang.ProductID <> '" . trim($artid) . "' AND ";
        }
        $row = parent::mysql_fetch_one_rows("
            SELECT count(ProductID) as total FROM
                " . prefix . "product_lang
            WHERE
                " . $str . "
                " . prefix . "product_lang.SeoKey = '" . trim($seokey) . "' AND
                " . prefix . "product_lang.LangID = '" . $lang_id . "'
        ");

        parent::mysql_close_connection();

        return isset($row['total']) ? $row['total'] : NULL;
    }

    public function mdp_prot_insert() {
        $data = array(
            'CID' => isset($_POST['CID']) && !empty($_POST['CID']) ? $_POST['CID'] : 0,
            'Order' => isset($_POST['Order']) && !empty($_POST['Order']) ? $_POST['Order'] : 0,
            'Manufacturer' => isset($_POST['Manufacturer']) && !empty($_POST['Manufacturer']) ? $_POST['Manufacturer'] : 0,
            'Code' => isset($_POST['Code']) && !empty($_POST['Code']) ? $_POST['Code'] : 0,
            'Size' => isset($_POST['Size']) && !empty($_POST['Size']) ? $_POST['Size'] : 0,
            'Price' => isset($_POST['Price']) && !empty($_POST['Price']) ? $_POST['Price'] : 0,
            'Discount' => isset($_POST['Discount']) && !empty($_POST['Discount']) ? $_POST['Discount'] : 0,
            'Percent' => isset($_POST['Percent']) && !empty($_POST['Percent']) ? $_POST['Percent'] : 0,
            'FromDate' => isset($_POST['FromDate']) && !empty($_POST['FromDate']) && $_POST['FromDate'] != 0 ? strtotime($_POST['FromDate']) : 0,
            'ToDate' => isset($_POST['ToDate']) && !empty($_POST['ToDate']) && $_POST['ToDate'] != 0 ? strtotime($_POST['ToDate']) : 0,
            'ProductSpecial' => isset($_POST['ProductHot']) && !empty($_POST['ProductHot']) ? 1 : 0,
            'ProductHighlight' => isset($_POST['ProductHot']) && !empty($_POST['ProductHot']) ? 1 : 0,
            'ProductHot' => isset($_POST['ProductHot']) && !empty($_POST['ProductHot']) ? 1 : 0,
            'NoIndexFollow' => isset($_POST['NoIndexFollow']) && !empty($_POST['NoIndexFollow']) ? 1 : 0,
            'DatePost' => isset($_POST['DatePost']) && !empty($_POST['DatePost']) ? strtotime($_POST['DatePost']) : 0,
            'ActiveDatePost' => isset($_POST['ActiveDatePost']) && !empty($_POST['ActiveDatePost']) ? 1 : 0,
            'PostDate' => (string) time(),
            'UserID' => $_SESSION['ID'],
            'LocationID' => isset($_POST['location']) && !empty($_POST['location']) ? $_POST['location'] : 0,
            'Picture' => isset($_POST['Img']) ? $_POST['Img'] : null
        );

        $rs = parent::mysql_insert_array(prefix . "product", $data);
        $id = mysql_insert_id();
        parent::mysql_close_connection();
        return $id;
    }

    public function mdp_prot_insert_lang($id, $lang) {
        $data = array(
            'ProductID' => $id,
            'LangID' => $lang,
            'Tabs' => $_POST['js_tabs'][$lang],
            'AltPic' => $_POST['AltPic'][$lang],
            'Name' => $_POST['Name'][$lang],
            'FullContents' => $_POST['FullContents'][$lang],
            'Locks' => isset($_POST['Locks'][$lang]) ? $_POST['Locks'][$lang] : 0,
            'SeoKey' => $_POST['SeoKey'][$lang],
            'Title' => $_POST['Title'][$lang],
            'Desc' => $_POST['Desc'][$lang],
            'Keyword' => $_POST['Keyword'][$lang],
            'TitleH1' => $_POST['TitleH1'][$lang],
            'TitleH2' => $_POST['TitleH2'][$lang],
            'DescF' => $_POST['DescF'][$lang],
            'Status' => $_POST['Status'][$lang]
        );
        $rs = parent::mysql_insert_array(prefix . "product_lang", $data);
        parent::mysql_close_connection();
        return $rs;
    }

    public function mdp_prot_update($id) {
        $data = array(
            'CID' => isset($_POST['CID']) && !empty($_POST['CID']) ? $_POST['CID'] : 0,
            'Order' => isset($_POST['Order']) && !empty($_POST['Order']) ? $_POST['Order'] : 0,
            'Manufacturer' => isset($_POST['Manufacturer']) && !empty($_POST['Manufacturer']) ? $_POST['Manufacturer'] : 0,
            'Code' => isset($_POST['Code']) && !empty($_POST['Code']) ? $_POST['Code'] : 0,
            'Size' => isset($_POST['Size']) && !empty($_POST['Size']) ? $_POST['Size'] : 0,
            'Price' => isset($_POST['Price']) && !empty($_POST['Price']) ? $_POST['Price'] : 0,
            'Discount' => isset($_POST['Discount']) && !empty($_POST['Discount']) ? $_POST['Discount'] : 0,
            'Percent' => isset($_POST['Percent']) && !empty($_POST['Percent']) ? $_POST['Percent'] : 0,
            'FromDate' => isset($_POST['FromDate']) && !empty($_POST['FromDate']) && $_POST['FromDate'] != 0 ? strtotime($_POST['FromDate']) : 0,
            'ToDate' => isset($_POST['ToDate']) && !empty($_POST['ToDate']) && $_POST['ToDate'] != 0 ? strtotime($_POST['ToDate']) : 0,
            'ProductSpecial' => isset($_POST['ProductHot']) && !empty($_POST['ProductHot']) ? 1 : 0,
            'ProductHighlight' => isset($_POST['ProductHot']) && !empty($_POST['ProductHot']) ? 1 : 0,
            'ProductHot' => isset($_POST['ProductHot']) && !empty($_POST['ProductHot']) ? 1 : 0,
            'NoIndexFollow' => isset($_POST['NoIndexFollow']) && !empty($_POST['NoIndexFollow']) ? 1 : 0,
            'DatePost' => isset($_POST['DatePost']) && !empty($_POST['DatePost']) ? strtotime($_POST['DatePost']) : 0,
            'ActiveDatePost' => isset($_POST['ActiveDatePost']) && !empty($_POST['ActiveDatePost']) ? 1 : 0,
            'EditDate' => (string) time(),
            'LocationID' => isset($_POST['location']) && !empty($_POST['location']) ? $_POST['location'] : 0,
            'UserID' => $_SESSION['ID']
        );
        if (isset($_POST['Img']) && !empty($_POST['Img'])) {
            $data['Picture'] = $_POST['Img'];
        }

        $rs = parent::mysql_update_array(prefix . "product", $data, " ID='" . $id . "' ");
        parent::mysql_close_connection();
        return $rs;
    }

    public function mdp_prot_update_lang($id, $lang) {
        $rs = null;

        $aid = $this->mdp_get_prot_lang($id, $lang);
        $xid = count($aid);

        if ($xid > 0) {
            $data = array(
                'Tabs' => $_POST['js_tabs'][$lang],
                'AltPic' => $_POST['AltPic'][$lang],
                'Name' => $_POST['Name'][$lang],
                'FullContents' => $_POST['FullContents'][$lang],
                'Locks' => isset($_POST['Locks'][$lang]) && !empty($_POST['Locks'][$lang]) ? $_POST['Locks'][$lang] : 0,
                'SeoKey' => $_POST['SeoKey'][$lang],
                'Title' => $_POST['Title'][$lang],
                'Desc' => $_POST['Desc'][$lang],
                'Keyword' => $_POST['Keyword'][$lang],
                'TitleH1' => $_POST['TitleH1'][$lang],
                'TitleH2' => $_POST['TitleH2'][$lang],
                'DescF' => $_POST['DescF'][$lang],
                'Status' => $_POST['Status'][$lang]
            );
            $rs = parent::mysql_update_array(prefix . "product_lang", $data, "ProductID='" . $id . "' AND LangID='" . $lang . "'");
        } else {
            $seo_key = $this->mdp_get_seo_key($_POST['SeoKey'][$lang], $lang);
            if ($seo_key < 1) {
                $data = array(
                    'ProductID' => $id,
                    'LangID' => $lang,
                    'AltPic' => $_POST['AltPic'][$lang],
                    'Name' => $_POST['Name'][$lang],
                    'FullContents' => $_POST['FullContents'][$lang],
                    'Locks' => isset($_POST['Locks'][$lang]) && !empty($_POST['Locks'][$lang]) ? $_POST['Locks'][$lang] : 0,
                    'SeoKey' => $_POST['SeoKey'][$lang],
                    'Title' => $_POST['Title'][$lang],
                    'Desc' => $_POST['Desc'][$lang],
                    'Keyword' => $_POST['Keyword'][$lang],
                    'TitleH1' => $_POST['TitleH1'][$lang],
                    'TitleH2' => $_POST['TitleH2'][$lang],
                    'DescF' => $_POST['DescF'][$lang],
                    'Status' => $_POST['Status'][$lang]
                );
                $rs = parent::mysql_insert_array(prefix . "product_lang", $data);
            }
        }

        parent::mysql_close_connection();
        return $rs;
    }

    public function mdp_prot_del_pic($id) {
        $id = parent::mysql_check_string($id);
        $update = "UPDATE " . prefix . "product SET Picture='' WHERE `ID` = '" . $id . "'";
        $done = parent::mysql_update_insert($update);
        parent::mysql_close_connection();
        return $done;
    }

    public function mdp_prot_get_pic($id) {
        $id = parent::mysql_check_string($id);
        $sql = "SELECT Picture FROM " . prefix . "product WHERE `ID` = '" . $id . "'";
        $row = parent::mysql_fetch_one_rows($sql);
        parent::mysql_close_connection();
        return isset($row['Picture']) ? $row['Picture'] : null;
    }

    public function mdp_tool_func($model, $active, $str_id) {
        $done = false;

        switch ($active) {
            case 'show':
            case 'hide':
            case 'del':
                $done = $model->model_tool_set_status(prefix . "product_lang", $active, " ProductID IN(" . trim($str_id) . ")");
                break;

            case 'delete':
                $arr = explode(',', $str_id);
                if (!empty($arr)) {
                    foreach ($arr as $id) {
                        $img = $this->mdp_prot_get_pic($id);
                        if (!empty($img))
                            unlink(PATH_BASE . 'upload/images/products/' . $img);
                    }
                }
                $done = $model->model_tool_delete(prefix . "product", " ID IN(" . trim($str_id) . ")");
                $done = $model->model_tool_delete(prefix . "product_lang", " ProductID IN(" . trim($str_id) . ")");

                break;
        }

        return $done;
    }

}